package _6_reflect_proxy.util;


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;

/**
 * @auther itw_liqt 2018/8/2
 */
public class ArgsCheck {
    private static final String GET_PREFIX = "get";
    private static final String SET_PREFIX = "set";


    /**
     * 校验 对象 的参数 不为空
     */
    public static void checkArgsNotEmpty(Object object, String... params) {
        if (object == null) {
            // logger.warn("invalid param, object  is null");
            throw new IllegalArgumentException("invalid object, object is null");
        }

        if (params.length==0) {
            return;
        }

        for (String param : params) {

            if (param == null || param.length() == 0) {
                continue;
            }

            String upperCaseName = param.substring(0, 1).toUpperCase() + param.substring(1);

            try {
                Method getMethod = object.getClass().getMethod(GET_PREFIX + upperCaseName);
                Object value = getMethod.invoke(object);
                if (value == null) {
                    // logger.warn("invalid param, param [" + param + "] is empty");
                    throw new IllegalArgumentException("invalid param, param [" + param + "] is empty");
                }

                if (value instanceof String) {
                    String str = (String) value;
                    if (str.length() == 0) {
                        // logger.warn("invalid param, param [" + param + "] is empty");
                        throw new IllegalArgumentException("invalid param, param [" + param + "] is empty");
                    }
                }

            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                // logger.info(e.toString());
            }
        }

    }

    /**
     * 校验 对象的所有属性都不为空
     */
    public static void checkAllFieldNotEmpty(Object object) {
        if (object == null) {
            // logger.warn("invalid param, object  is null");
            throw new IllegalArgumentException("invalid object, object is null");
        }

        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            // 去掉 final 和 transient修饰符修饰的属性
            if (Modifier.isFinal(field.getModifiers()) ||
                    Modifier.isTransient(field.getModifiers())) {
                continue;
            }

            String name = field.getName();
            String upperCaseName = name.substring(0, 1).toUpperCase() + name.substring(1);

            try {
                Method getMethod = object.getClass().getMethod(GET_PREFIX + upperCaseName);
                Object value = getMethod.invoke(object);
                if (value == null) {
                    // logger.warn("invalid param, param [" + name + "] is empty");
                    throw new IllegalArgumentException("invalid param, param [" + name + "] is empty");
                }

                if (field.getType() == String.class) {
                    String str = (String) value;
                    if (str.length() == 0) {
                        // logger.warn("invalid param, param [" + name + "] is empty");
                        throw new IllegalArgumentException("invalid param, param [" + name + "] is empty");
                    }
                }

            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                // logger.error(e.getMessage());
            }
        }
    }

    /**
     * 校验 map 里的参数不为空
     */
    public static void checkMapKVNotEmpty(Map<String, String> map, String... params) {
        if (map == null) {
            throw new IllegalArgumentException("invalid object, object is null");
        }
        if (params.length == 0) {
            return;
        }

        for (String param : params) {
            if (param == null) {
                continue;
            }
            if ((!map.containsKey(param)) || map.get(param).length() == 0) {
                // logger.warn("invalid param, param [" + param + "] is empty");
                throw new IllegalArgumentException("invalid param, param [" + param + "] is empty");
            }
        }


    }
}
